from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer

class CustomResponse:
    """
    自定义响应类
    统一处理API响应格式，确保所有接口返回统一的数据结构
    """
    @staticmethod
    def success(data=None, msg="成功", status=200, token=None):
        """
        成功响应方法
        参数：
            data: 响应数据
            msg: 响应消息
            status: HTTP状态码
            token: 认证令牌
        返回：
            Response对象，包含统一格式的响应数据
        """
        response_data = {
            "status": status,
            "msg": msg,
            "data": data if data is not None else "NULL",
            "token": token if token is not None else "NULL"
        }

        # return Response(response_data, status=status)
        response = Response(response_data, status=status)
        response.accepted_renderer = JSONRenderer()
        response.accepted_media_type = "application/json"
        response.renderer_context = {}
        response.render()
        return response







    @staticmethod
    def error(msg="error", status=400):
        """
        错误响应方法
        :param msg: 错误消息
        :param status: HTTP状态码
        :return: Response对象
        """
        response_data = {
            "status": status,
            "msg": msg,
            "data": "NULL",
            "token": "NULL"
        }

        # return Response(response_data, status=status)
        response = Response(response_data, status=status)
        response.accepted_renderer = JSONRenderer()
        response.accepted_media_type = "application/json"
        response.renderer_context = {}
        response.render()
        return response



